package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import feign.Param;
import io.swagger.v3.core.util.Json;
import jdk.jfr.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	/**
	 * 查询所有分类（1、2、3级分类）
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "album:category:list")
	public List<JSONObject> getBaseCategoryList() {
		//创建集合封装数据
		List<JSONObject> list = new ArrayList<>();

		//1.查询base_category_view视图 获取所有分类
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//2.根据分类编号(一级分类id)进行分组,并封装到一个map集合中进行遍历获取 categoryName categoryId
		baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id)).entrySet().forEach(entry -> {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("categoryId", entry.getKey());
			jsonObject.put("categoryName", entry.getValue().get(0).getCategory1Name());
			//3.categoryChild,里面是二级分类的信息同样的方法进行分组遍历存入集合中
			List<JSONObject> jsonObject2List = new ArrayList<>();
			entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id)).entrySet().forEach(entry2 -> {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());

				//4.categoryChild,里面是三级分类信息,直接遍历放入集合中
				List<JSONObject> jsonObject3List = new ArrayList<>();
				entry2.getValue().stream().forEach(baseCategoryView -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				});
				jsonObject2.put("categoryChild", jsonObject3List);
				jsonObject2List.add(jsonObject2);
			});
			jsonObject.put("categoryChild", jsonObject2List);
			list.add(jsonObject);
		});
		//2.1将数据

		return list;
	}

	/**
	 * 根据一级分类Id获取分类属性以及属性值（标签名，标签值）列表
	 * @param category1Id
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "album:category:attribute")
	public List<BaseAttribute> findAttribute(@Param("category1Id") Long category1Id) {
		List<BaseAttribute> list = baseAttributeMapper.selectAttributeListByCategoryId(category1Id);
		return list;
	}

	/**
	 * 根据三级分类查询分类列表
	 * @param category3Id
	 * @return
	 */
	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
		return baseCategoryView;
	}

	/**
	 * 根据一级分类Id查询置顶7个三级分类列表
	 * @param category1Id
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "album:category:top3:")
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//1.根据一级分类Id查询二级分类列表
		List<BaseCategory2> category2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1Id).select(BaseCategory2::getId));
		//2.获取二级分类的id
		List<Long> category2IdList = category2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		//3.根据二级分类的id获取三级分类列表
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(
				new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id, category2IdList)
						.eq(BaseCategory3::getIsTop, 1)
						.orderByAsc(BaseCategory3::getOrderNum)
						.last("limit 7")
		);
		return baseCategory3List;
	}

	/**
	 * 查询1级分类下包含所有二级以及三级分类
	 * @param category1Id
	 * @return
	 */
	@Override
	@GuiGuCache(prefix = "album:category:list")
	public JSONObject getBaseCategoryListCategoryById1(Long category1Id) {
		//1.通过视图BaseCategoryView查询获取该一级分类下的所有信息
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(
				new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id)
		);
		if (CollectionUtil.isNotEmpty(baseCategoryViewList)){
			//2.创建返回对象,封装数据也是用于一级分类的封装
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId", baseCategoryViewList.get(0).getCategory1Id());
			jsonObject1.put("categoryName", baseCategoryViewList.get(0).getCategory1Name());

			//3.创建集合用于一级分类categoryChild的封装
			List<JSONObject> jsonObjectList2 = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			map2.entrySet().forEach(entry -> {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry.getValue().get(0).getCategory2Id());
				jsonObject2.put("categoryName", entry.getValue().get(0).getCategory2Name());
				//创建一个集合用于封装二级分类的categoryChild
				List<JSONObject> jsonObjectList3 = new ArrayList<>();
				entry.getValue().stream().forEach(baseCategoryView -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					jsonObjectList3.add(jsonObject3);
				});
				jsonObject2.put("categoryChild", jsonObjectList3);
				jsonObjectList2.add(jsonObject2);
			});
			jsonObject1.put("categoryChild", jsonObjectList2);
			return jsonObject1;
		}
		return null;
	}
}
